package com.cg.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping("/mono")
@Slf4j
public class MonoController {


    @GetMapping("/hello")
    public Mono<String> hello(){
        return Mono.just("hello world");
    }

    @GetMapping("/testStr1")
    public Mono<String> testStr1() throws InterruptedException {
        log.info("start01");
        Mono<String> result = Mono.fromSupplier(() -> {
            String str = null;
            try {
                str = createStr();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return str;
        });
//        Mono<String> just = Mono.just(createStr());
        log.info("end01");
        return result;
    }

    private String createStr() throws InterruptedException {
        Thread.sleep(2000);
        return "哈哈哈哈哈";
    }

    @GetMapping("/testStr2")
    public String testStr2() throws InterruptedException {
        log.info("start02");
        String result = createStr();
        log.info("end02");
        return result;
    }

}
